package com.wujunshen.dumpling.core.engine;

import com.wujunshen.dumpling.common.utils.Assert;
import lombok.Getter;
import lombok.SneakyThrows;

import java.lang.reflect.Constructor;

/**
 * 生成构造工厂
 *
 * @author frankwoo
 */
@Getter
public class EngineFactory {
    /** EngineConfig */
    private final EngineConfig engineConfig;

    public EngineFactory(EngineConfig configuration) {
        Assert.isNotNull(configuration);
        this.engineConfig = configuration;
    }

    /**
     * 获取配置的数据库类型实例
     *
     * @return {@link AbstractTemplateEngine} 数据库查询对象
     */
    @SneakyThrows
    public AbstractTemplateEngine newInstance() {
        // 获取实现类
        Class<? extends AbstractTemplateEngine> query =
                this.engineConfig.getProduceType().getImplClass();
        // 获取有参构造
        Constructor<? extends AbstractTemplateEngine> constructor =
                query.getConstructor(EngineConfig.class);
        // 实例化
        return constructor.newInstance(engineConfig);
    }
}
